{ "cells": [ { "cell_type": "markdown", "id": "aa79e617-6b6b-4620-8c4f-808d3fca1566", "metadata": {}, "source": [ "# Parametric models\n", "Instead of setting numeric values as transition probabilities, we may also use parameters, polynomials or even rational functions in multiple variables." ] }, { "cell_type": "code", "execution_count": 1, "id": "386ffe0b-99fb-4bc4-ad93-7d5bebe3b206", "metadata": {}, "outputs": [], "source": [ "from stormvogel import parametric" ] }, { "cell_type": "markdown", "id": "e16876b3-7b6a-4555-9dd3-25015453836b", "metadata": {}, "source": [ "Polynomials are represented as dictionaries where the keys are the exponents and the values are coefficients. In addition, we must also supply a list of variable names. Rational functions are then represented as a pair of two polynomials (numerator and denominator)." ] }, { "cell_type": "code", "execution_count": 2, "id": "497f79e8-f2e1-4fe8-a64b-fba502301549", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x^2 + y^2\n", "2.0*1 + z + 6.0*z^3\n", "(x^2 + y^2)/(2.0*1 + z + 6.0*z^3)\n" ] } ], "source": [ "polynomial1 = parametric.Polynomial([\"x\",\"y\"])\n", "polynomial1.add_term((2,0),1)\n", "polynomial1.add_term((0,2),1)\n", "\n", "print(polynomial1)\n", "\n", "polynomial2 = parametric.Polynomial([\"z\"])\n", "polynomial2.add_term((0,),2)\n", "polynomial2.add_term((1,),1)\n", "polynomial2.add_term((3,),6)\n", "\n", "print(polynomial2)\n", "\n", "rational_function = parametric.RationalFunction(polynomial1, polynomial2)\n", "\n", "print(rational_function)" ] }, { "cell_type": "markdown", "id": "424d1c58-805f-4f52-8601-4caafa06a6ea", "metadata": {}, "source": [ "To create a parametric model (e.g. pmc or pmdp) we simply have to set such a value as a transition probability. As an example, we provide the knuth yao dice, but with parameters instead of concrete probabilities." ] }, { "cell_type": "code", "execution_count": 6, "id": "9ac1f65e-ff50-49d9-9064-e7ab71b4036b", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "fa52ffb6b6fe47ada4bfced55c053173", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output()" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "4c5639ad766c4125b677b4004f0e4a5d", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output()" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n", "\n", " \n", " Network\n", " \n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from stormvogel import model, pgc\n", "from stormvogel.show import show\n", "\n", "#we first make polynomials 'x' and '1-x'\n", "x = parametric.Polynomial([\"x\"])\n", "x.add_term((1,),1)\n", "\n", "invx = parametric.Polynomial([\"x\"])\n", "invx.add_term((1,),-1)\n", "invx.add_term((0,),1)\n", "\n", "#we build the knuth yao dice using the pgc model builder\n", "def delta(s: pgc.State):\n", " match s.s:\n", " case 0:\n", " return [(x, pgc.State(s=1)), (invx, pgc.State(s=2))]\n", " case 1:\n", " return [(x, pgc.State(s=3)), (invx, pgc.State(s=4))]\n", " case 2:\n", " return [(x, pgc.State(s=5)), (invx, pgc.State(s=6))]\n", " case 3:\n", " return [(x, pgc.State(s=1)), (invx, pgc.State(s=7, d=1))]\n", " case 4:\n", " return [\n", " (x, pgc.State(s=7, d=2)),\n", " (invx, pgc.State(s=7, d=3)),\n", " ]\n", " case 5:\n", " return [\n", " (x, pgc.State(s=7, d=4)),\n", " (invx, pgc.State(s=7, d=5)),\n", " ]\n", " case 6:\n", " return [(x, pgc.State(s=2)), (invx, pgc.State(s=7, d=6))]\n", " case 7:\n", " return [(1, s)]\n", "\n", "def labels(s: pgc.State):\n", " if s.s == 7:\n", " return f\"rolled{str(s.d)}\"\n", "\n", "knuth_yao = pgc.build_pgc(\n", " delta=delta,\n", " initial_state_pgc=pgc.State(s=0),\n", " labels=labels,\n", " modeltype=model.ModelType.DTMC,\n", ")\n", "\n", "vis = show(knuth_yao)" ] }, { "cell_type": "markdown", "id": "0d03e392-6724-44f8-b97f-3fa1f9c48218", "metadata": {}, "source": [ "We can now evaluate the model by assigning the variable x to any concrete value. This induces a regular dtmc with fixed probabilities." ] }, { "cell_type": "code", "execution_count": 18, "id": "b4057fab-76a6-4b00-99d5-b3c835aee9ba", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c0fa5acfaaac40e3a32080bcd1267e11", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output()" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "7134dd0e121f4f6cb1eb10a7ede64ba8", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output()" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n", "\n", " \n", " Network\n", " \n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "p = 1/2\n", "\n", "eval_knuth_yao = knuth_yao.parameter_valuation({\"x\":p})\n", "vis = show(eval_knuth_yao)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.7" } }, "nbformat": 4, "nbformat_minor": 5 }